package com.sinoglobal.ningxia.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.sinoglobal.ningxia.R;
import com.sinoglobal.ningxia.utils.ItktLog;

/**
 * 
 * @author limeishu
 * @createdate 2014-6-17 下午5:11:37
 * @Description: TODO(滑动开关)
 */
public class ToggleSlipButton extends View implements OnTouchListener {
	private Context context;
	private boolean NowChoose;// 记录当前按钮是否打开,true为打开,flase为关闭
	private boolean isChecked;
	private boolean OnSlip = false;// 记录用户是否在滑动的变量
	private float DownX, NowX;// 按下时的x,当前的x
	private Rect Btn_On, Btn_Off;// 打开和关闭状态下,游标的Rect .
	private boolean isChgLsnOn = false;
	private OnChangedListener ChgLsn;
	public Bitmap off_bg, on_bg, slip_btn;

	public ToggleSlipButton(Context context) {
		super(context);
		init();
	}

	public ToggleSlipButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public ToggleSlipButton(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		this.context = context;
		init();
	}

	private void init() {// 初始化
		off_bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg_toggle_off);
		on_bg = BitmapFactory.decodeResource(getResources(), R.drawable.bg_toggle_on);
		slip_btn = BitmapFactory.decodeResource(getResources(), R.drawable.tonggle_button);
		Btn_On = new Rect(0, 0, slip_btn.getWidth(), slip_btn.getHeight());
		Btn_Off = new Rect(on_bg.getWidth() - slip_btn.getWidth(), 0, on_bg.getWidth(), slip_btn.getHeight());
		ItktLog.i("--toggle-->" + on_bg.getWidth() + "-->" + slip_btn.getWidth());
		setOnTouchListener(this);// 设置监听器,也可以直接复写OnTouchEvent
	}

	@Override
	protected void onDraw(Canvas canvas) {// 绘图函数
		super.onDraw(canvas);
		Matrix matrix = new Matrix();
		Paint paint = new Paint();
		float x;
		if (NowX < (off_bg.getWidth() / 2))// 滑动到前半段与后半段的背景不同,在此做判断
		{
			x = NowX - slip_btn.getWidth() / 2;
			canvas.drawBitmap(on_bg, matrix, paint);// 画出关闭时的背景
		} else {
			x = off_bg.getWidth() - slip_btn.getWidth() / 2;
			canvas.drawBitmap(off_bg, matrix, paint);// 画出打开时的背景
		}
		if (OnSlip)// 是否是在滑动状态,
		{
			if (NowX >= off_bg.getWidth())// 是否划出指定范围,不能让游标跑到外头,必须做这个判断
				x = off_bg.getWidth() - slip_btn.getWidth() / 2;// 减去游标1/2的长度...
			else if (NowX < 0) {
				x = 0;
			} else {
				x = NowX - slip_btn.getWidth() / 2;
			}
		} else {// 非滑动状态
			if (NowChoose)// 根据现在的开关状态设置画游标的位置
			{
				x = Btn_Off.left;
				canvas.drawBitmap(off_bg, matrix, paint);// 初始状态为true时应该画出打开状态图片
			} else
				x = Btn_On.left;
		}
		if (isChecked) {
			canvas.drawBitmap(off_bg, matrix, paint);
			x = Btn_Off.left;
			isChecked = !isChecked;
		}
		if (x < 0)// 对游标位置进行异常判断...
			x = 0;
		else if (x > off_bg.getWidth() - slip_btn.getWidth())
			x = off_bg.getWidth() - slip_btn.getWidth();
		canvas.drawBitmap(slip_btn, x, 0, paint);// 画出游标.
	}

	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction())
		// 根据动作来执行代码
		{
		case MotionEvent.ACTION_MOVE:// 滑动
			NowX = event.getX();
			break;
		case MotionEvent.ACTION_DOWN:// 按下
			if (event.getX() > off_bg.getWidth() || event.getY() > off_bg.getHeight())
				return false;
			OnSlip = true;
			DownX = event.getX();
			NowX = DownX;
			break;
		case MotionEvent.ACTION_CANCEL: // 移到控件外部
			OnSlip = false;
			boolean choose = NowChoose;
			if (NowX >= (off_bg.getWidth() / 2)) {
				NowX = off_bg.getWidth() - slip_btn.getWidth() / 2;
				NowChoose = true;
			} else {
				NowX = NowX - slip_btn.getWidth() / 2;
				NowChoose = false;
			}
			if (isChgLsnOn && (choose != NowChoose)) // 如果设置了监听器,就调用其方法..
				ChgLsn.OnChanged(NowChoose);
			break;
		case MotionEvent.ACTION_UP:// 松开
			OnSlip = false;
			boolean LastChoose = NowChoose;
			if (event.getX() >= (off_bg.getWidth() / 2)) {
				NowX = off_bg.getWidth() - slip_btn.getWidth() / 2;
				NowChoose = true;
			} else {
				NowX = NowX - slip_btn.getWidth() / 2;
				NowChoose = false;
			}
			if (isChgLsnOn && (LastChoose != NowChoose)) // 如果设置了监听器,就调用其方法..
				ChgLsn.OnChanged(NowChoose);
			break;
		default:
		}
		invalidate();// 重画控件
		return true;
	}

	public void SetOnChangedListener(OnChangedListener l) {// 设置监听器,当状态修改的时候
		isChgLsnOn = true;
		ChgLsn = l;
	}

	public interface OnChangedListener {
		abstract void OnChanged(boolean CheckState);
	}

	//改变开关状态， state为true 代表开， false代表关
	public void setOn2Off(boolean state, boolean NowChose, boolean ischecked) {
		if (state) {
			NowX = -off_bg.getWidth();
		} else {
			NowX = on_bg.getWidth();
		}
		NowChoose = NowChose;
		isChecked = ischecked;
		invalidate();

	}

	public void setCheck(boolean isChecked) {
		if (isChecked) {
			NowX = on_bg.getWidth();
		} else {
			NowX = -off_bg.getWidth();
		}
		NowChoose = isChecked;
		invalidate();
	}

	public Bitmap getOff_bg() {
		return off_bg;
	}

	public void setOff_bg(Bitmap off_bg) {
		this.off_bg = off_bg;
	}

	public Bitmap getOn_bg() {
		return on_bg;
	}

	public void setOn_bg(Bitmap on_bg) {
		this.on_bg = on_bg;
	}

	public Bitmap getSlip_btn() {
		return slip_btn;
	}

	public void setSlip_btn(Bitmap slip_btn) {
		this.slip_btn = slip_btn;
	}

}